
package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Clases.Usuario;

/**
 *
 * @author Pavilion
 */
@WebServlet(name = "ServletIngresar", urlPatterns = {"/ServletIngresar"})
public class ServletIngresar extends HttpServlet {

    HttpSession miSesion = null;
    private static final long serialVersionUID = 1L;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();
                try {
                    Usuario elUsuario = new Usuario();
                    int status = elUsuario.login(request.getParameter("usuario"),request.getParameter("clave"));
                    switch(status) {
                        case 0:
                                this.miSesion =  request.getSession(true);
                                this.miSesion.setMaxInactiveInterval(120);
                                String miusuario= request.getParameter("usuario");
                                this.miSesion.setAttribute("usuario",miusuario);
                                this.miSesion.setAttribute("elUsuario",elUsuario);
                                this.miSesion.setAttribute("denominacion",elUsuario.getDenominacion());
                                this.miSesion.setAttribute("idUsuario", elUsuario.getId_usuario());
                                this.miSesion.setAttribute("documento", elUsuario.getDocumento());
                                this.miSesion.setAttribute("Perfil", elUsuario.getPerfil());
                                String pru = elUsuario.getPerfil();
                                if ("AS".equals(pru)) {
                                    response.sendRedirect("AS.jsp");
                                }else if ("CD".equals(pru)) {
                                    response.sendRedirect("CD.jsp");
                                }else if ("SOC".equals(pru)) {
                                    response.sendRedirect("Socio.jsp");
                                }else if ("NAU".equals(pru)) {
                                    response.sendRedirect("NAU_.jsp");
                                }else if ("TES".equals(pru)) {
                                    response.sendRedirect("Tesoreria.jsp");
                                }else if ("AD".equals(pru)) {
                                    response.sendRedirect("Administrador.jsp");
                                }
                                break;
                        case 1:
                                response.sendRedirect("index.jsp?status=1");
                                break;
                        }
                } finally {
                    out.close();
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

